package com.vdf.sisconsolid.persistencemodule.converter;

import java.util.ArrayList;
import java.util.List;

import com.vdf.sisconsolid.persistencemodule.dto.CellDTO;
import com.vdf.sisconsolid.persistencemodule.dto.KeyValueBean;
import com.vdf.sisconsolid.persistencemodule.model.Address;
import com.vdf.sisconsolid.persistencemodule.model.Cell;
import com.vdf.sisconsolid.util.CacheSC;
import com.vdf.sisconsolid.util.UtilSC;

public class CellConverter {

	public static List<KeyValueBean> toCellKeyValue(List<Cell> cellList) {
		List<KeyValueBean> cellBeanList = new ArrayList<KeyValueBean>();
		for (Cell cell : cellList) {
			KeyValueBean pBean = new KeyValueBean();
			pBean.setKey(cell.getId());
			pBean.setValue(cell.getPrittyName());

			cellBeanList.add(pBean);
		}
		return cellBeanList;

	}

	public static List<CellDTO> toDTOList(List<Cell> cells) {
		List<CellDTO> cellDTOList = new ArrayList<CellDTO>();
		for (Cell cell : cells) {
			cellDTOList.add(toDTO(cell));
		}
		return cellDTOList;
	}

	public static CellDTO toDTO(Cell cell) {
		// cell
		CellDTO cDTO = new CellDTO();
		cDTO.setIdCell(cell.getId());
		cDTO.setStartDate(cell.getStartDate());
		cDTO.setLeaderFirstName(cell.getLeaderFirstName());
		cDTO.setLeaderLastName(cell.getLeaderLastName());
		cDTO.setLeaderPhone(cell.getLeaderPhone());
		cDTO.setHostName(cell.getHostName());
		cDTO.setCellPhone(cell.getCellPhone());
		cDTO.setDay(cell.getDay());
		cDTO.setDayName(CacheSC.getDays().get(cell.getDay()));
		cDTO.setTime(cell.getTime());
		cDTO.setPostalCode(cell.getPostalCode());
		cDTO.setCellCode(cell.getCellCode());
		cDTO.setGeneration(cell.getGeneration());

		if (cell.getCell() != null) {
			cDTO.setIdMotherCell(cell.getCell().getId());
			cDTO.setMotherCell(cell.getCell().getPrittyName());
		}

		cDTO.setIdFamily(cell.getFamily().getId());
		cDTO.setFamily(cell.getFamily().getName());

		cDTO.setIdNet(cell.getNet().getId());
		cDTO.setNet(cell.getNet().getName());

		Address address = cell.getAddress();
		cDTO.setIdAddress(address.getId());
		cDTO.setIdProvince(address.getProvince().getId());
		cDTO.setProvince(address.getProvince().getName());
		cDTO.setIdCity(address.getCity().getId());
		cDTO.setCity(address.getCity().getName());
		if (address.getNeighborhood() != null) {
			cDTO.setIdNeighborhood(address.getNeighborhood().getId());
			cDTO.setNeighborhood(address.getNeighborhood().getName());
		}
		if (address.getStreet() != null) {
			cDTO.setIdStreet(address.getStreet().getId());
			cDTO.setStreet(address.getStreet().getName());
		}
		cDTO.setNumber(address.getNumber());
		cDTO.setFloor(address.getFloor());
		cDTO.setDepartment(address.getDepartment());
		cDTO.setLot(address.getLot());
		cDTO.setBlock(address.getBlock());
		String prittyAddress = UtilSC.generatePrittyAddress(cDTO.getStreet(),
				cDTO.getNumber(), cDTO.getFloor(), cDTO.getDepartment(),
				cDTO.getBlock(), cDTO.getLot(), cDTO.getNeighborhood(),
				cDTO.getProvince(), cDTO.getCity());
		cDTO.setPrittyAddress(prittyAddress);

		return cDTO;
	}

	public static Cell toEntity(CellDTO cDTO) {

		// cell
		Cell cell = new Cell();
		cell.setId(cDTO.getIdCell());
		cell.setStartDate(cDTO.getStartDate());
		cell.setLeaderFirstName(cDTO.getLeaderFirstName());
		cell.setLeaderLastName(cDTO.getLeaderLastName());
		cell.setLeaderPhone(cDTO.getLeaderPhone());
		cell.setHostName(cDTO.getHostName());
		cell.setCellPhone(cDTO.getCellPhone());
		cell.setDay(cDTO.getDay());
		cell.setTime(cDTO.getTime());
		cell.setPostalCode(cDTO.getPostalCode());
		cell.setCellCode(cDTO.getCellCode());
		cell.setGeneration(cDTO.getGeneration());

		cell.setIdFamily(cDTO.getIdFamily());
		cell.setIdNet(cDTO.getIdNet());
		cell.setIdMotherCell(cDTO.getIdMotherCell());

		Address address = new Address();
		address.setId(cDTO.getIdAddress());
		address.setIdProvince(cDTO.getIdProvince());
		address.setIdCity(cDTO.getIdCity());
		address.setIdNeighborhood(cDTO.getIdNeighborhood());
		address.setIdStreet(cDTO.getIdStreet());
		address.setNumber(cDTO.getNumber());
		address.setFloor(cDTO.getFloor());
		address.setDepartment(cDTO.getDepartment());
		address.setLot(cDTO.getLot());
		address.setBlock(cDTO.getBlock());
		cell.setAddress(address);

		return cell;
	}
}
